package com.xyht.sca_s.student_manage_system.modules.carUseManage.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.carUseManage.entity.req.SmsCarUseApplicationAllowReq;
import com.xyht.sca_s.student_manage_system.modules.carUseManage.entity.req.SmsCarUseApplicationCancelReq;
import com.xyht.sca_s.student_manage_system.modules.carUseManage.entity.req.SmsCarUseApplicationExportReq;
import com.xyht.sca_s.student_manage_system.modules.carUseManage.entity.req.SmsCarUseApplicationReq;
import com.xyht.sca_s.student_manage_system.modules.carUseManage.service.SmsCarUseApplicationService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-07-31
 */
@SecretBody
@RestController
@RequestMapping("/carUseManage")
public class SmsCarUseApplicationController {
    @Autowired
    SmsCarUseApplicationService smsCarUseApplicationService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "提交车辆使用申请")
    @PostMapping("/add")
    public ResponseResult addApplication(@RequestBody SmsCarUseApplicationReq applicationReq,
                                         HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsCarUseApplicationService.addApplication(applicationReq,userId);
    }

    @ApiModelProperty(value = "撤销车辆使用申请")
    @PostMapping("/cancel")
    public ResponseResult cancelApplication(@RequestBody SmsCarUseApplicationCancelReq cancelReq,
                                            HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsCarUseApplicationService.cancelApplication(cancelReq,userId);
    }

    @ApiModelProperty(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult getList(@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                  @RequestParam(value = "search_str",required = false) String search_str,
                                  @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "apply_type") Integer apply_type,
                                  HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsCarUseApplicationService.getList(pageSize,search_str,pageNum,apply_type,userId);
    }
    @ApiModelProperty(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult getDetail(@RequestParam(value = "apply_id") String apply_id,
                                    HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsCarUseApplicationService.getDetail(apply_id,userId);
    }

    @ApiModelProperty("审批")
    @PostMapping("/allow")
    public ResponseResult allowApplication(@RequestBody SmsCarUseApplicationAllowReq allowVO,
                                         HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsCarUseApplicationService.allowApplication(allowVO,user_id);
    }


    @ApiModelProperty("申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getCarUseApplicationOriginSettingProcess() {
        return smsCarUseApplicationService.getCarUseApplicationOriginSettingProcess();
    }
    @ApiModelProperty(value = "查询所有申请记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllCarUseApply(
            @RequestParam(value = "apply_unit",required = false) String apply_unit,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum
    ){
        return smsCarUseApplicationService.selectAllCarUseApply(apply_unit, pageSize,pageNum);
    }

    @ApiModelProperty(value = "PDF文件下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "apply_id") String apply_id,
                                        HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsCarUseApplicationService.tableDownload(apply_id,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;

    }

    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportCarUseApplication(@RequestBody SmsCarUseApplicationExportReq exportReq,
                                          HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsCarUseApplicationService.exportCarUseApplication(exportReq, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "根据借用时间段查询车辆信息")
    @GetMapping("/selectByTime")
    public ResponseResult selectByTime(
                                    @RequestParam(value = "useStartTime", required = false) String useStartTime,
                                    @RequestParam(value = "useEndTime", required = false) String useEndTime){
        return smsCarUseApplicationService.selectByTime(useStartTime,useEndTime);
    }
}

